using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using ApiTest.Api.Entity;
using System.Linq;
using ApiTest.Api.Db;
using ApiTest.Api.Repository;
using ApiTest.Api.Params;
using Microsoft.Extensions.Configuration;
using ApiTest.Api.Utils;
using Microsoft.AspNetCore.Authorization;
using Admin3000.Backend.Api.Params;

namespace ApiTest.Api.Controllers
{
    // [Authorize]
    [ApiController]
    [Route("[controller]")]
    public class ShopingCarController : ControllerBase
    {
        private IConfiguration _configuration;
        private IRepository<ShopingCar> _ShopingCarRepository;
        private IRepository<Spuspecification> _SpuspecificationRepository;
        private TokenParameter _tokenParameter;
        private IRepository<Spu> _SpuRepository;
        private IRepository<Spuspecificationvalue> _SpuspecificationvalueRepository;
        public ShopingCarController(
        IConfiguration configuration,
        IRepository<ShopingCar> ShopingCarRepository,
        IRepository<Spu> SpuRepository,
        IRepository<Spuspecification> SpuspecificationRepository,
        IRepository<Spuspecificationvalue> SpuspecificationvalueRepository
        )
        {
            _configuration = configuration;
            _SpuRepository = SpuRepository;
            _ShopingCarRepository = ShopingCarRepository;
            _SpuspecificationRepository = SpuspecificationRepository;
            _SpuspecificationvalueRepository = SpuspecificationvalueRepository;
            _tokenParameter = _configuration.GetSection("TokenParameter").Get<TokenParameter>();
        }

        [HttpGet]
        public dynamic Get()
        {
            var ShopingCar = _ShopingCarRepository.Table.ToList();

            return new
            {
                Code = 1000,
                Data = ShopingCar,
                Msg = "查询评论表成功"
            };
        }

        [HttpGet("{id}")]
        public dynamic Get(int id)
        {
            var order = _ShopingCarRepository.GetById(id);

            return new
            {
                Code = 1000,
                Data = order,
                Msg = "查询订单成功"
            };
        }


        [HttpPost]
        public dynamic Post(CreateShopingCar newShopingCar)
        {
            var ShopingCar = new ShopingCar
            {
                userId = newShopingCar.userId,
                spuId = newShopingCar.spuId,
                spuTotal = newShopingCar.spuTotal,
                total = newShopingCar.total,
                totaPrice = newShopingCar.totaPrice,
            };
            _ShopingCarRepository.Insert(ShopingCar);
            return new
            {
                Code = 1000,
                Data = ShopingCar,
                Msg = "添加订单成功"
            };
        }


        //连接分类表和细分类表
        [HttpGet("yonhuai/{id}"), Route("yonhuai")]
        public dynamic Specificationname(int id)
        {
            var Spuspecificationvalue = _SpuspecificationvalueRepository.Table.ToList();
            var Spuspecification = _SpuspecificationRepository.Table.ToList();
            var ShopingCar = _ShopingCarRepository.Table.ToList();

            var Spu = _SpuRepository.Table.ToList();
            var query = from a in Spu
                        join b in ShopingCar on a.Id equals b.userId
                        select new
                        {
                            spuId = a.Id,
                            Spuname = a.Spuname,
                            Price = a.Price,
                            ImageId = a.ImageId,
                            Num = a.Num,
                            Intro = a.Intro,
                            UserId = b.userId,
                            spuTotal = b.spuTotal,
                        };
            var specificationvalue = ShopingCar.Where(x => x.userId == id).ToList();
            // var Spuspecification = query.Where(x=>x.spuId ==Spuspecification.SpuId).ToList();
            return new
            {
                Code = 1000,
                Data = new { Data = query },
                Msg = "查询指定用户Id成功"
            };


        }
        [HttpDelete("{id}")]
        public dynamic Delete(int id)
        {
            _ShopingCarRepository.Delete(id);

            return new
            {
                Code = 1000,
                Data = "",
                Msg = "删除成功"
            };


        }

    }

}
